// 获取session中的用户id
var cusId = sessionStorage.getItem("cusid");

//页面加载时调用全部收藏的获取方法
$(function() {
    getAllTravelers();
});

//==========================1=========================
//发送ajax请求获取所有的常用旅客信息
//将获得到的返回数据渲染到页面中

function getAllTravelers() {

    var sendUrl = "/travelers/customer/" + cusId;

    $.ajax({

        url : sendUrl,
        type : "GET",
        datatype : "json",
        success : function(data) {
            console.log(data);

            var travelers = data.data;

            //清空分页插件div和订单列表div里的内容
            $(".com_t_lists").empty();
            $("#page").empty();

            //重新导入layui分页插件
            layui.use('laypage',function () {
                var laypage = layui.laypage;

                //执行一个laypage实例
                laypage.render({
                    elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                    ,count: travelers.length //数据总数，从服务端得到
                    ,limit: 3  //每页显示的条数。laypage将会借助 count 和 limit 计算出分页数。
                    , curr: 1
                    // , first: 1
                    ,layout: ['prev', 'page', 'next','count']
                    ,jump : function (obj,first) {

                        //清空旅客信息列表里的内容
                        $(".com_t_lists").empty();
                        console.log("分页成功");

                        var pageCount = obj.count; //总页数
                        var pageNo = obj.curr;  //当前页数
                        var evePageCount = obj.limit; //每页数量

                        var finalCount; //末值区间

                        if (pageNo * evePageCount >= pageCount) {
                            finalCount = pageCount;
                        } else {
                            finalCount = pageNo*evePageCount;
                        }

                        for (var i = (pageNo-1)*evePageCount; i< finalCount;i++){

                            var mytra = travelers[i];

                            //开始渲染
                            $(".com_t_lists").append(
                                "<div class=\"t_info\">\n" +
                                "                            <ul>\n" +
                                "                                <li class=\"th_name\">姓名</li>\n" +
                                "                                <li class=\"th_type\">证件类型</li>\n" +
                                "                                <li class=\"th_IdCard\">证件号码</li>\n" +
                                "                                <li class=\"th_tel\">手机号码</li>\n" +
                                "                                <li class=\"th_do\">操作</li>\n" +
                                "                            </ul>\n" +
                                "                            <ul>\n" +
                                "                                <li class=\"th_name name\">"+mytra.travellerName+"</li>\n" +
                                "                                <li class=\"th_type type\">"+mytra.certificateType+"</li>\n" +
                                "                                <li class=\"th_IdCard IdCard\">"+mytra.certificateNo+"</li>\n" +
                                "                                <li class=\"th_tel tel\">"+mytra.travellerTelno+"</li>\n" +
                                "                                <li class=\"th_do do\">\n" +
                                "                                    <a href='javascript:void(0);' class=\"edit_a\" onclick='goEdit(event)'>修改</a>\n" +
                                "                                    <span class=\"t_id\" style='visibility: hidden'>"+mytra.id+"</span>\n" +
                                "                                </li>\n" +
                                "                            </ul>\n" +
                                "                            <ul class=\"other_info\">\n" +
                                "                                <li>\n" +
                                "                                    <span>性别:&nbsp;&nbsp;&nbsp;</span>\n" +
                                "                                    <span>" + mytra.travellerGender + "</span>\n" +
                                "                                </li>\n" +
                                "                                <li>\n" +
                                "                                    <span>旅客类型:&nbsp;&nbsp;&nbsp;</span>\n" +
                                "                                    <span>" + mytra.travellerType + "</span>\n" +
                                "                                </li>\n" +
                                "                            </ul>\n" +
                                "                        </div>"
                            )
                        }
                    }
                })
            })
        } //success 方法
    })
}

//==========================1=========================
//点击修改时的操作
function goEdit(event) {
    var eve = event.target;
    var travelId = $(eve).next().text();
    window.sessionStorage.setItem("travelid",travelId);
    $(location).attr('href', 'travelers_edit');
}

//=============================================================================
//                              根据旅客姓名查询
//=============================================================================
$('.query_btn').click(function () {
    queryByTName();
});

function queryByTName() {
    var tName = $('.query_input').val().trim();
    console.log(tName);
    console.log(tName.length);

    var sendUrl = "/travelers/get/" + tName;

    if (tName.length > 0) {
        $.ajax({
            url : sendUrl,
            type : "GET",
            datatype : "json",
            success : function(data) {
                console.log(data);

                var travelers = data.data;

                //清空分页插件div和订单列表div里的内容
                $(".com_t_lists").empty();
                $("#page").empty();

                //重新导入layui分页插件
                layui.use('laypage',function () {
                    var laypage = layui.laypage;

                    //执行一个laypage实例
                    laypage.render({
                        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                        ,count: travelers.length //数据总数，从服务端得到
                        ,limit: 3  //每页显示的条数。laypage将会借助 count 和 limit 计算出分页数。
                        , curr: 1
                        // , first: 1
                        ,layout: ['prev', 'page', 'next','count']
                        ,jump : function (obj,first) {

                            //清空旅客信息列表里的内容
                            $(".com_t_lists").empty();
                            console.log("分页成功");

                            var pageCount = obj.count; //总页数
                            var pageNo = obj.curr;  //当前页数
                            var evePageCount = obj.limit; //每页数量

                            var finalCount; //末值区间

                            if (pageNo * evePageCount >= pageCount) {
                                finalCount = pageCount;
                            } else {
                                finalCount = pageNo*evePageCount;
                            }

                            for (var i = (pageNo-1)*evePageCount; i< finalCount;i++){

                                var mytra = travelers[i];

                                //开始渲染
                                $(".com_t_lists").append(
                                    "<div class=\"t_info\">\n" +
                                    "                            <ul>\n" +
                                    "                                <li class=\"th_name\">姓名</li>\n" +
                                    "                                <li class=\"th_type\">证件类型</li>\n" +
                                    "                                <li class=\"th_IdCard\">证件号码</li>\n" +
                                    "                                <li class=\"th_tel\">手机号码</li>\n" +
                                    "                                <li class=\"th_do\">操作</li>\n" +
                                    "                            </ul>\n" +
                                    "                            <ul>\n" +
                                    "                                <li class=\"th_name name\">"+mytra.travellerName+"</li>\n" +
                                    "                                <li class=\"th_type type\">"+mytra.certificateType+"</li>\n" +
                                    "                                <li class=\"th_IdCard IdCard\">"+mytra.certificateNo+"</li>\n" +
                                    "                                <li class=\"th_tel tel\">"+mytra.travellerTelno+"</li>\n" +
                                    "                                <li class=\"th_do do\">\n" +
                                    "                                    <a href='javascript:void(0);' class=\"edit_a\" onclick='goEdit(event)'>修改</a>\n" +
                                    "                                    <span class=\"t_id\" style='visibility: hidden'>"+mytra.id+"</span>\n" +
                                    "                                </li>\n" +
                                    "                            </ul>\n" +
                                    "                            <ul class=\"other_info\">\n" +
                                    "                                <li>\n" +
                                    "                                    <span>性别:&nbsp;&nbsp;&nbsp;</span>\n" +
                                    "                                    <span>" + mytra.travellerGender + "</span>\n" +
                                    "                                </li>\n" +
                                    "                                <li>\n" +
                                    "                                    <span>旅客类型:&nbsp;&nbsp;&nbsp;</span>\n" +
                                    "                                    <span>" + mytra.travellerType + "</span>\n" +
                                    "                                </li>\n" +
                                    "                            </ul>\n" +
                                    "                        </div>"
                                )
                            }
                        }
                    })
                })
            }
        })
    }
    else {
        layui.use('layer', function(){
            var layer = layui.layer;

            layer.msg('输入的内容不能为空');
        });
    }


}


//点击名称进入首页
$(".nav_userName").click(function () {

    $.ajax({
        url: "/order/getId",
        type: "GET",
        datatype: "json",
        success: function (data) {
            console.log(data.data)
            window.sessionStorage.setItem("cusid", data.data)
            $(location).attr('href', 'order');
        }

    })

})

